import { AudioClip, AudioSource, resources } from "cc"

export class AudioManager {
  // 存储音效的 AudioSource 实例
  private audioSources: { [key: string]: AudioSource } = {}
  // 存储当前背景音乐的 AudioSource 实例
  private backgroundAudioSource: AudioSource | null = null

  private constructor() {}

  // 单例模式获取实例
  private static _instance: AudioManager = null
  public static get instance() {
    if (!AudioManager._instance) AudioManager._instance = new AudioManager()
    return AudioManager._instance
  }

  // 加载并播放背景音乐
  public playMusic(musicName: string, loop = true, volume = 1): void {
    // 若已有背景音乐在播放，先停止它
    if (this.backgroundAudioSource) {
      this.backgroundAudioSource.stop()
    }
    resources.load(`sounds/${musicName}`, AudioClip, (err, clip: AudioClip) => {
      if (err) {
        console.error(`Failed to load background music: ${musicName}`, err)
        return
      }
      if (!this.backgroundAudioSource) {
        this.backgroundAudioSource = new AudioSource()
      }
      this.backgroundAudioSource.clip = clip
      this.backgroundAudioSource.loop = loop
      this.backgroundAudioSource.volume = volume
      this.backgroundAudioSource.play()
    })
  }

  // 停止背景音乐
  public stopMusic(): void {
    if (this.backgroundAudioSource) {
      this.backgroundAudioSource.stop()
    }
  }

  // 暂停背景音乐
  public pauseMusic(): void {
    if (this.backgroundAudioSource) {
      this.backgroundAudioSource.pause()
    }
  }

  // 恢复背景音乐
  public resumeMusic(): void {
    if (this.backgroundAudioSource) {
      this.backgroundAudioSource.play()
    }
  }

  // 加载并播放音效
  public playEffect(effectName: string, loop = false, volume = 1): void {
    resources.load(`sounds/${effectName}`, AudioClip, (err, clip: AudioClip) => {
      if (err) {
        console.error(`Failed to load sound effect: ${effectName}`, err)
        return
      }
      const audioSource = new AudioSource()
      audioSource.clip = clip
      audioSource.loop = loop
      audioSource.volume = volume
      audioSource.play()
      // 存储音效的 AudioSource 实例，方便后续管理
      this.audioSources[effectName] = audioSource
    })
  }

  // 停止指定音效
  public stopEffect(effectName: string): void {
    const audioSource = this.audioSources[effectName]
    if (audioSource) {
      audioSource.stop()
    }
  }

  // 暂停指定音效
  public pauseEffect(effectName: string): void {
    const audioSource = this.audioSources[effectName]
    if (audioSource) {
      audioSource.pause()
    }
  }

  // 恢复指定音效
  public resumeEffect(effectName: string): void {
    const audioSource = this.audioSources[effectName]
    if (audioSource) {
      audioSource.play()
    }
  }

  // 停止所有音效
  public stopAllEffects(): void {
    for (const key in this.audioSources) {
      if (this.audioSources.hasOwnProperty(key)) {
        this.audioSources[key].stop()
      }
    }
  }

  // 暂停所有音效
  public pauseAllEffects(): void {
    for (const key in this.audioSources) {
      if (this.audioSources.hasOwnProperty(key)) {
        this.audioSources[key].pause()
      }
    }
  }

  // 恢复所有音效
  public resumeAllEffects(): void {
    for (const key in this.audioSources) {
      if (this.audioSources.hasOwnProperty(key)) {
        this.audioSources[key].play()
      }
    }
  }
}
